package com.madou.dbtool.redis.panel;

import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.event.DBEvent;
import com.madou.dbtool.event.EventPanel;
import com.madou.dbtool.redis.event.OpenRedisDataEvent;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.panel.ui.RedisDataPanel;
import com.madou.dbtool.ui.content.TitleNo;

import javax.swing.*;
import java.awt.*;

/**
 * redis的终端慢日志监控面板
 * @author miukoo
 */
@HideClass
public class RedisDataEventPanel extends JPanel implements EventPanel, TitleNo {
    String title;
    //
    RedisDataPanel redisDataPanel;

    public RedisDataEventPanel(){
        this.setLayout(new BorderLayout());
    }

    public static DBEvent bingEvent() {
        return OpenRedisDataEvent.NAME;
    }

    @Override
    public void actionEvent(DBEvent event) {
        if(event instanceof OpenRedisDataEvent){
            OpenRedisDataEvent e = (OpenRedisDataEvent)event;
            RedisSourceManager clone = e.getRedisSourceManager().cloneDirect();
            this.redisDataPanel = new RedisDataPanel(clone,e.getKey(),e.getDbIndex());
            this.title = String.format("[%d]View-%s",e.getDbIndex(),e.getRedisSourceManager().getDataSourceName());
            this.title = this.getTitle(title);
            this.redisDataPanel.init();
            this.add(redisDataPanel,BorderLayout.CENTER);
            clone.execute(e.getDbIndex(),"select "+e.getDbIndex());
        }
    }

    @Override
    public String title() {
        return title;
    }

    /**
     * 是否可以滚动
     * @return
     */
    @Override
    public boolean canScroll(){
        return false;
    }

    @Override
    public Icon icon(){
        return AllIcons.Common.TABLE;
    }

    /**
     * 关闭按钮的回调函数
     * @return
     */
    @Override
    public void onClose(){
        this.clearTitle(title);
        this.redisDataPanel.desroty();
    }

}
